package com.micro.frog.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.frog.common.base.PageRes;
import com.micro.frog.common.base.Result;
import com.micro.frog.rbac.entity.DictDO;
import com.micro.frog.rbac.mapper.DictMapper;
import com.micro.frog.rbac.service.DictService;
import com.micro.frog.rbac.vo.DictVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :  ljh
 * @date : 2018/4/11
 **/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, DictDO> implements DictService {

    @Override
    public List<DictDO> queryList(DictDO dict) {
        QueryWrapper<DictDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", "N");
        if (StringUtils.isNotBlank(dict.getType())) {
            queryWrapper.eq("`type`", dict.getType());
        }
        return this.list(queryWrapper);
    }

    @Override
    public Result<PageRes<DictDO>> queryPage(DictVO dictVo) {

        DictDO dict = new DictDO();
        BeanUtils.copyProperties(dictVo, dict);
        QueryWrapper<DictDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dict);
        IPage<DictDO> iPage = this.page(new Page<>(dictVo.getPageNum(), dictVo.getPageSize()));
        return Result.success(PageRes.success(iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getRecords()));
    }

    @Override
    public boolean delete(Long id) {
        DictDO dict = new DictDO();
        dict.setId(id);
        dict.setIsDeleted("Y");
        return this.updateById(dict);
    }

    @Override
    public boolean update(DictDO dict) {
        return this.updateById(dict);
    }
}
